<template>
  <div class="login">
    <auth-form @onSubmit="onSubmit"></auth-form>
  </div>
</template>

<script>
import AuthForm from "@/components/AuthForm.vue";
import myAxios from "@/utils/httpUtil";
import UserUtil from "@/utils/UserUtil";
import { Toast } from "vant";

export default {
  name: "LoginPage",
  components: {
    AuthForm,
  },
  methods: {
    onSubmit(values) {
      console.log(values);

      myAxios
        .post("/h5/user/login", {
          username: values.username,
          password: values.password,
        })
        .then((res) => {
          if (res.data.code === 10000) {
            console.log(res.data.data);
            UserUtil.setUser({
              token: res.data.data.token,
              username: res.data.data.currentAuthority,
            });

            // 获取重定向路径
            const redirect = this.$route.query.redirect || "/";
            // 跳转到重定向路径
            console.log(redirect);
            this.$router.replace(redirect);
          }
        })
        .catch((error) => {
          Toast(error.response.data.message);
          console.log(error);
        });
    },
  },
};
</script>

<style></style>
